home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Miscellaneous
/
Headlines Code
/
Headlines Project ƒ
/
HeadlinesMain.c
< prev
next >
Wrap
Text File
|
1992-11-23
|
9KB
|
394 lines
/*
* HeadlinesMain.c
*
* This is © Copyright 1992 by Jamie R. McCarthy. All rights reserved.
*
* Please note that, in Headlines 1.0, I simply repeated Greg Smith's
* distribution agreement, namely that no monetary gain must be made
* from this program. In this version, I'm not distributing his source
* code, and am changing the terms a little. Headlines and its source
* code may not be sold by themselves, but they may be included in any
* collection of software, as long as that collection, if it contains
* shareware programs, prominently notifies the purchaser of the
* additional monetary obligation for those programs. Apart from that,
* use and distribute them freely!
*
* If you would like to examine Greg's original spew source code, you can
* probably obtain it from the same place I did: the wuarchive.wustl.edu
* ftp archive, in /usenet/comp.sources.games/volume01.
*/
/******************************/
#include "HeadlinesMain.h"
#include "JMassMalloc.h"
#include "Spyoo.h"
#include "SpyooDisplay.h"
/******************************/
#define kPStrUnknownError ("\pUnknown error.")
#define kInitialPauseTicks (90)
#define kOffScreenSpeedFactor (150)
#define kOnScreenSpeedFactor (600)
#define kMinimumNTicks (6)
#define kSnuggleSpeedFactor (4)
#define kMinNTicksToSnuggle (1)
#define kMaxNTicksToSnuggle (60)
#define kLengthSpeedFactor (9)
/******************************/
/* A global for the storage, so I don't have to keep passing it around. */
Handle *gStorage;
/* The input resource (corresponds to "InFile"), its mark, and its length. */
char **gInRsrcHndl;
long gInRsrcMark;
long gInRsrcLength;
/* Is a headline presently being displayed? */
Boolean gHeadlineIsUp;
/* The time at which the last action--display or removal--took place. */
unsigned long gLastActionTicks;
/* The current line being read in. */
char gInLine[256];
/* How many classes there are. */
short gNClasses;
/* A pointer to the array of class records. */
class *gClass;
/* The default tags, which are shared. */
char gNullTags[] = " ";
/* The data for JRandom. */
jrStruct gJR;
/* The headline that will be, is, or was being displayed. */
Str255 gHeadline;
/* Are we working with TrueType? */
Boolean gHasTrueType;
/* Have all the allocations been freed? */
Boolean gHasFreedHeadlinesAllocations = FALSE;
/******************************/
OSErr doInitialize(Handle *storage,
RgnHandle blankRgn,
short message,
GMParamBlockPtr params)
{
long theResponse;
OSErr theOSErr;
theOSErr = noErr;
gStorage = storage;
gInRsrcHndl = NULL;
gInRsrcMark = 0;
gLastActionTicks = TickCount() + kInitialPauseTicks;
gHeadlineIsUp = FALSE;
gFontClass.weight = 0;
gFontClass.list = NULL;
gFontClass.name = NULL;
gFontClass.tags = NULL;
theOSErr = Gestalt(gestaltFontMgrAttr, &theResponse);
if (theOSErr != noErr) {
/* A pre-6.0.4 system; no TrueType. Forgive the error. */
gHasTrueType = FALSE;
theOSErr = noErr;
} else {
gHasTrueType = ( (theResponse & gestaltOutlineFonts) != 0);
}
/* If storage has already been allocated, something fishy's going on! */
if (*gStorage != NULL) {
return storageAlreadyInitedErr;
}
setupJMM();
IJRandom(&gJR);
/* Load in the resource handle. */
SetResLoad(FALSE);
gInRsrcHndl = (char**) GetResource('TEXT', 128);
SetResLoad(TRUE);
if (gInRsrcHndl == NULL) {
theOSErr = noHeadlinesErr;
} else {
LoadResource(gInRsrcHndl);
if (*gInRsrcHndl == NULL || ResError() != noErr) {
theOSErr = memFullErr;
} else {
gInRsrcLength = GetHandleSize(gInRsrcHndl);
HNoPurge(gInRsrcHndl);
}
}
/* Make a dummy allocation, in case After Dark gets suspicious. */
if (theOSErr == noErr && *gStorage == NULL) {
*gStorage = NewHandle( 0L );
if (*gStorage == NULL) {
theOSErr = memFullErr;
}
}
if (theOSErr == noErr) {
theOSErr = setupDisplay();
}
freeHeadlinesAllocationsIfErr(theOSErr);
return theOSErr;
}
OSErr doClose(Handle *storage,
RgnHandle blankRgn,
short message,
GMParamBlockPtr params)
{
gStorage = storage;
freeHeadlinesAllocations();
shutdownDisplay(params);
return noErr;
}
OSErr doBlank(Handle *storage,
RgnHandle blankRgn,
short message,
GMParamBlockPtr params)
{
OSErr theOSErr;
theOSErr = noErr;
gStorage = storage;
PenPat(params->qdGlobalsCopy->qdBlack);
PenMode(patCopy);
PaintRgn(blankRgn);
if (theOSErr == noErr && gInRsrcHndl != NULL) {
HLock(gInRsrcHndl);
theOSErr = readText();
HUnlock(gInRsrcHndl);
ReleaseResource( (Handle) gInRsrcHndl);
gInRsrcHndl = NULL;
}
freeHeadlinesAllocationsIfErr(theOSErr);
return theOSErr;
}
OSErr doDrawFrame(Handle *storage,
RgnHandle blankRgn,
short message,
GMParamBlockPtr params)
{
OSErr theOSErr;
unsigned long nextActionTicks;
short ticksPastDeadline;
short speedControlValue;
gStorage = storage;
theOSErr = noErr;
speedControlValue = 100 - params->controlValues[kSpeedControl];
nextActionTicks = gLastActionTicks
+ kMinimumNTicks
+ (long)speedControlValue *
(gHeadlineIsUp ?
(kOnScreenSpeedFactor + (long)gHeadline[0]*kLengthSpeedFactor)
: (kOffScreenSpeedFactor)
) / 100;
ticksPastDeadline = TickCount() - nextActionTicks;
if (ticksPastDeadline >= 0) {
if (gHeadlineIsUp) {
theOSErr = remove(params, blankRgn);
if (theOSErr == noErr) {
gHeadlineIsUp = FALSE;
gLastActionTicks = TickCount();
}
} else {
unsigned short maxNTicksToSnuggle;
Boolean mustDrawNow;
maxNTicksToSnuggle =
speedControlValue * kSnuggleSpeedFactor
+ kMinNTicksToSnuggle;
if (maxNTicksToSnuggle > kMaxNTicksToSnuggle) {
maxNTicksToSnuggle = kMaxNTicksToSnuggle;
}
mustDrawNow = (ticksPastDeadline > maxNTicksToSnuggle+4);
theOSErr = display(params, blankRgn, "MAIN/ ", ' ',
maxNTicksToSnuggle, mustDrawNow);
if (theOSErr == noErr) {
gHeadlineIsUp = TRUE;
gLastActionTicks = TickCount();
} else if (theOSErr == retryDisplayErr) {
/*
* Do nothing--try to display it again the next time around.
*/
theOSErr = noErr;
}
}
}
freeHeadlinesAllocationsIfErr(theOSErr);
return theOSErr;
}
OSErr doModuleSelected(Handle *storage,
RgnHandle blankRgn,
short message,
GMParamBlockPtr params)
{
return noErr;
}
OSErr doAbout(Handle *storage,
RgnHandle blankRgn,
short message,
GMParamBlockPtr params)
{
return noErr;
}
OSErr doButtonMessage(Handle *storage,
RgnHandle blankRgn,
short message,
GMParamBlockPtr params)
{
return noErr;
}
void setADErrorText(Handle *storage,
RgnHandle blankRgn,
short message,
GMParamBlockPtr params,
OSErr theOSErr)
{
adErrPStrCpy(params, "\pHeadlines: ");
switch (theOSErr) {
case noErr:
break;
case ModuleError:
adErrPStrCat(params, "\pModule error.");
break;
case memFullErr:
adErrPStrCat(params, "\pOut of memory.");
break;
case expectedClassDefnErr:
adErrPStrCat(params, "\pExpected a class definition.");
break;
case expectedClassInstanceErr:
adErrPStrCat(params, "\pExpected a class instance.");
break;
case tooManyClassesErr:
adErrPStrCat(params, "\pToo many classes.");
break;
case badClassHeaderErr:
adErrPStrCat(params, "\pA bad class header was found.");
break;
case badLineWeightErr:
adErrPStrCat(params, "\pA bad line weight was found.");
break;
case noHeadlinesErr:
adErrPStrCat(params, "\pNo headlines were found.");
break;
case noMainClassErr:
adErrPStrCat(params, "\pNo class named MAIN was found.");
break;
case storageAlreadyInitedErr:
adErrPStrCat(params, "\pStorage was already initialized.");
break;
case noFontInfoErr:
adErrPStrCat(params, "\pNo font info resource was found.");
break;
case badFontInfoErr:
adErrPStrCat(params, "\pThe font info resource is bad.");
break;
case unknownErr:
default: {
Str15 theValString;
adErrPStrCat(params, "\pUnknown error, #");
NumToString(theOSErr, theValString);
adErrPStrCat(params, theValString);
adErrPStrCat(params, "\p.");
} break;
}
}
void freeHeadlinesAllocations(void)
{
if (gHasFreedHeadlinesAllocations) return;
gHasFreedHeadlinesAllocations = TRUE;
if (*gStorage != NULL) {
DisposHandle(*gStorage);
*gStorage = NULL;
}
if (gInRsrcHndl != NULL) {
ReleaseResource( (Handle) gInRsrcHndl);
gInRsrcHndl = NULL;
}
shutdownJMM();
}
void freeHeadlinesAllocationsIfErr(OSErr theOSErr)
{
if (theOSErr != noErr) freeHeadlinesAllocations();
}